package com.ly.airline.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.airline.pojo.Hangban;
import com.ly.airline.service.HangbanService;
import com.ly.airline.utils.HttpResult;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author ：xxl
 * @ClassName HangbanController
 * @date ：Created in 2022/4/28 16:08
 * @description： 航班接口
 * @modified By：
 * @version: 1.0
 */
@Controller
@RequestMapping("hangban")
public class HangbanController {

    @Autowired
    private HangbanService hangbanService;


    /**
     *@param current
	* @param pageSize
	* @param hangban
	* @param model
     * @return java.lang.String
     * @author xxl
     * @description: 后台得到航班列表
     * @date 2022/4/30 0:18
     */
    @GetMapping("getHangbanList/{current}/{pageSize}")
    public String getHangbanList(@PathVariable Integer current, @PathVariable Integer pageSize, Hangban hangban, Model model){
        //设置分页
        IPage page = new Page(current,pageSize);
        //设置查询条件
        LambdaQueryWrapper<Hangban> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotBlank(hangban.getShifadi()),Hangban::getShifadi,hangban.getShifadi())
                .like(Strings.isNotBlank(hangban.getDaodadi()),Hangban::getDaodadi,hangban.getDaodadi());
        //分页查询
        hangbanService.page(page,queryWrapper);
        model.addAttribute("pagers",page);
        //返回查询条件
        model.addAttribute("obj",hangban);
        return "admin/hangban/hangbanMana";
    }

    /**
     *@param current
	* @param pageSize
	* @param hangban
	* @param model
     * @return java.lang.String
     * @author xxl
     * @description: 前台获取航班列表
     * @date 2022/4/30 0:19
     */
    @GetMapping("getAllHangban/{current}/{pageSize}")
    public String getAllHangban(@PathVariable Integer current, @PathVariable Integer pageSize, Hangban hangban, Model model){
        //设置分页
        IPage page = new Page(current,pageSize);
        //涉资查询条件
        LambdaQueryWrapper<Hangban> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotBlank(hangban.getShifadi()),Hangban::getShifadi,hangban.getShifadi())
                .like(Strings.isNotBlank(hangban.getDaodadi()),Hangban::getDaodadi,hangban.getDaodadi())
                .eq(Strings.isNotBlank(hangban.getRiqi()),Hangban::getRiqi,hangban.getRiqi());
        //航班分页查询
        hangbanService.page(page,queryWrapper);
        model.addAttribute("pagers",page);
        //返回查询条件
        model.addAttribute("obj",hangban);
        return "qiantai/hangban/hangbanAll";
    }

    /**
     *@param id
     * @return com.ly.airline.utils.HttpResult
     * @author xxl
     * @description: 删除航班
     * @date 2022/4/30 0:20
     */
    @GetMapping("deleteHangban/{id}")
    @ResponseBody
    public HttpResult deleteHangban(@PathVariable Integer id){
        hangbanService.removeById(id);
        return HttpResult.success(true,"删除成功");
    }

    /**
     *@param
     * @return java.lang.String
     * @author xxl
     * @description: 后台跳转到添加航班页面
     * @date 2022/4/30 0:21
     */
    @GetMapping("toAddHangban")
    public String toAddHangban(){
        return "admin/hangban/hangbanAdd";
    }

    /**
     *@param hangban
     * @return com.ly.airline.utils.HttpResult
     * @author xxl
     * @description: 添加航班
     * @date 2022/4/30 0:22
     */
    @ResponseBody
    @PostMapping("addHangban")
    public HttpResult addHangban(@RequestBody Hangban hangban){
        //校验数据完整性
        if (Strings.isBlank(hangban.getDaodadi())||Strings.isBlank(hangban.getShifadi())||Strings.isBlank(hangban.getBianhao())){
            return HttpResult.success(false,"请填写完整信息");
        }
        if (Strings.isBlank(hangban.getQifeishi())||Strings.isBlank(hangban.getRiqi())){
            return HttpResult.success(false,"请填写完整信息");
        }
        //设置查询条件
        LambdaQueryWrapper<Hangban> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Hangban::getDaodadi,hangban.getDaodadi())
                .eq(Hangban::getShifadi,hangban.getShifadi())
                .eq(Hangban::getQifeishi,hangban.getQifeishi())
                .eq(Hangban::getRiqi,hangban.getRiqi());
        //查询是否已有航班
        Hangban one = hangbanService.getOne(queryWrapper);
        if (BeanUtil.isNotEmpty(one)){
            return HttpResult.success(true,"已有航班");
        }


        //添加航班
        hangbanService.save(hangban);
        return HttpResult.success(true,"添加成功");
    }

    /**
     *@param id
	* @param model
     * @return java.lang.String
     * @author xxl
     * @description: 获取航班详情
     * @date 2022/4/30 0:25
     */
    @GetMapping("getHangbanDetail/{id}")
    public String getHangbanDetail(@PathVariable Integer id,Model model){
        Hangban hangban = hangbanService.getById(id);
        model.addAttribute("hangban",hangban);
        return "qiantai/hangban/hangbanDetailQian";
    }

    /**
     *@param id
	* @param model
     * @return java.lang.String
     * @author xxl
     * @description: 跳转到购票页面
     * @date 2022/4/30 0:25
     */
    @GetMapping("toBuyTicket/{id}")
    public String toBuyTicket(@PathVariable Integer id,Model model){
        model.addAttribute("hangbanId",id);
        return "qiantai/hangban/dingpiaoAdd";
    }
}
